#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:

    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int m, n;

    vector<vector<int>> pacificAtlantic(vector<vector<int>>& h) {
        m = h.size(), n = h[0].size();
        vector<vector<bool>>pac(m, vector<bool>(n));
        vector<vector<bool>>atl(m, vector<bool>(n));
        for (int j = 0; j < n; j++) dfs(h, 0, j, pac);
        for (int i = 0; i < m; i++) dfs(h, i, 0, pac);
        for (int j = 0; j < n; j++) dfs(h, m - 1, j, atl);
        for (int i = 0; i < m; i++) dfs(h, i, n - 1, atl);
        vector<vector<int>>ret;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (pac[i][j] && atl[i][j])
                {
                    ret.push_back({ i,j });
                }
            }
        }
        return ret;
    }
    void dfs(vector<vector<int>>& h, int i, int j, vector<vector<bool>>& sign)
    {
        sign[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !sign[x][y] && h[x][y] >= h[i][j])
            {
                dfs(h, x, y, sign);
            }
        }
    }
};